using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VEPROMS.CSLA.Library;
using System.IO;

namespace jcbTestAudits
{
	public delegate void ItemRestoredHandler(ItemInfo restoredItemInfo);

	public partial class DisplayHistory : UserControl
	{
		public event ItemRestoredHandler ItemRestored;
		private void OnItemRestored(ItemInfo restoredItemInfo)
		{
			if (ItemRestored != null) ItemRestored(restoredItemInfo);
		}

		public DisplayHistory()
		{
			InitializeComponent();
		}
		private ItemInfo _MyItemInfo;

		public ItemInfo MyItemInfo
		{
			get { return _MyItemInfo; }
			set 
			{ 
				_MyItemInfo = value;
				UpdateHistory();
			}
		}

		private bool _AnnotationOnly = false;

		public bool AnnotationOnly
		{
			get { return _AnnotationOnly; }
			set 
			{ 
				_AnnotationOnly = value;
				UpdateHistory();
			}
		}

		private void UpdateHistory()
		{
			#region setup
			btnRestore.Enabled = false;
			myRTB.Clear();
			myVFG.Clear();
			tvAudits.Nodes.Clear();
			if (MyItemInfo == null) return;
			MyItemInfo.RefreshItemAnnotations();
			#endregion
			//if (AnnotationOnly)
			//{
			#region annotation deleted
			//annotation deleted by itemid
			TreeNode annotationDeleted = null;
			AnnotationAuditInfoList aail = AnnotationAuditInfoList.GetByItemID(MyItemInfo.ItemID);
			foreach (AnnotationAuditInfo aai in aail)
			{
				TreeNode tn = null;
				if (aai.DeleteStatus > 0)
				{
					if (annotationDeleted == null)
						annotationDeleted = tvAudits.Nodes.Add("Deleted Annotations");
					if (tn == null)
					{
						AnnotationTypeInfo ati = AnnotationTypeInfo.Get(aai.TypeID);
						tn = annotationDeleted.Nodes.Add(ati.Name);
					}
					TreeNode cn = tn.Nodes.Add(aai.ToString());
					cn.Tag = aai;
				}
			}
			//end annotation deleted
			#endregion
			#region annotation changes
			//annotation change by annotationid
			TreeNode annotationHistory = null;
			if (MyItemInfo.ItemAnnotationCount > 0)
			{
				foreach (AnnotationInfo ai in MyItemInfo.ItemAnnotations)
				{
					TreeNode tn = null;
					aail = AnnotationAuditInfoList.GetByAnnotationID(ai.AnnotationID);
					foreach (AnnotationAuditInfo aai in aail)
					{
						if (aai.DTS != ai.DTS)
						{
							if (annotationHistory == null)
								annotationHistory = tvAudits.Nodes.Add("Annotation History");
							if (tn == null)
								tn = annotationHistory.Nodes.Add(string.Format("{0} - {1}", ai.MyAnnotationType.Name, ai.SearchText));
							TreeNode cn = tn.Nodes.Add(aai.ToString());
							cn.Tag = aai;
						}
					}
				}
			}
			//end annotation change
			#endregion
			//  return;
			//}
			#region content changes
			//content changes
			TreeNode contentHistory = null;
			if (MyItemInfo == null) return;
			ContentAuditInfoList cail = ContentAuditInfoList.Get(MyItemInfo.ContentID);
			ContentInfo ci = ContentInfo.Get(MyItemInfo.ContentID);
			foreach (ContentAuditInfo cai in cail)
			{
				if (cai.DeleteStatus == 0)
				{
					if (cai.DTS != ci.DTS)
					{
						if (contentHistory == null)
							contentHistory = tvAudits.Nodes.Add("Content History");
						TreeNode tn = contentHistory.Nodes.Add(cai.ToString());
						tn.Tag = cai;
					}
				}
			}
			//end content changes
			#endregion
			#region items
			TreeNode previousItem = null;
			TreeNode nextItem = null;
			TreeNode procedurePart = null;
			TreeNode sectionPart = null;
			TreeNode cautionPart = null;
			TreeNode notePart = null;
			TreeNode rnoPart = null;
			TreeNode stepPart = null;
			TreeNode tablePart = null;
			#endregion
			ItemAuditInfoList iail = ItemAuditInfoList.Get(MyItemInfo.ItemID);
			foreach (ItemAuditInfo iai in iail)
			{
				switch (iai.Level)
				{
					#region previous item
					case 0:	//previous item
						{
							if (previousItem == null)
								previousItem = tvAudits.Nodes.Add("Previous Item");
							TreeNode tn = previousItem.Nodes.Add(iai.ToString());
							tn.Tag = iai;
							break;
						}
					#endregion
					#region next item
					case 1:	//next item
						{
							if (nextItem == null)
								nextItem = tvAudits.Nodes.Add("Next Item");
							TreeNode tn = nextItem.Nodes.Add(iai.ToString());
							tn.Tag = iai;
							break;
						}
					#endregion
					case 2:	//parts
						{
							PartAuditInfoList pail = null;
							pail = PartAuditInfoList.GetByDeleteStatus(iai.DeleteStatus);
							if (pail.Count == 0)
								pail = PartAuditInfoList.GetByItemID(iai.ItemID);
							foreach (PartAuditInfo pai in pail)
							{
								if (pai.ContentID == MyItemInfo.ContentID)
								{
									switch (pai.FromType)
									{
										#region procedure part
										case 1:	//procedures
											{
												if (procedurePart == null)
													procedurePart = tvAudits.Nodes.Add("Procedures");
												TreeNode tn = procedurePart.Nodes.Add(iai.ToString());
												tn.Tag = iai;
												break;
											}
										#endregion
										#region section part
										case 2:	//sections
											{
												if (sectionPart == null)
													sectionPart = tvAudits.Nodes.Add("Sections");
												TreeNode tn = sectionPart.Nodes.Add(iai.ToString());
												tn.Tag = iai;
												break;
											}
										#endregion
										#region caution part
										case 3:	//cautions
											{
												if (cautionPart == null)
													cautionPart = tvAudits.Nodes.Add("Cautions");
												TreeNode tn = cautionPart.Nodes.Add(iai.ToString());
												tn.Tag = iai;
												break;
											}
										#endregion
										#region note part
										case 4:	//notes
											{
												if (notePart == null)
													notePart = tvAudits.Nodes.Add("Notes");
												TreeNode tn = notePart.Nodes.Add(iai.ToString());
												tn.Tag = iai;
												break;
											}
										#endregion
										#region rno part
										case 5:	//rnos
											{
												if (rnoPart == null)
													rnoPart = tvAudits.Nodes.Add("RNOs");
												TreeNode tn = rnoPart.Nodes.Add(iai.ToString());
												tn.Tag = iai;
												break;
											}
										#endregion
										#region step part
										case 6:	//steps
											{
												if (stepPart == null)
													stepPart = tvAudits.Nodes.Add("Steps");
												TreeNode tn = stepPart.Nodes.Add(iai.ToString());
												tn.Tag = iai;
												break;
											}
										#endregion
										#region table part
										case 7:	//tables
											{
												if (tablePart == null)
													tablePart = tvAudits.Nodes.Add("Tables");
												TreeNode tn = tablePart.Nodes.Add(iai.ToString());
												tn.Tag = iai;
												break;
											}
										#endregion
									}
								}
							}
							break;
						}
				}
			}
		}

		private void DisplayTagRTF(TreeNode tn)
		{
			if (tn.Tag != null)
			{
				if (tn.Tag is ContentAuditInfo)
				{
					btnRestore.Enabled = true;
					myVFG.Clear();
					ContentAuditInfo cai = tn.Tag as ContentAuditInfo;
					GridAuditInfoList gail = GridAuditInfoList.Get(cai.ContentID);
					if (gail.Count > 0)
					{
						foreach (GridAuditInfo gai in gail)
						{
							if (gai.DTS == cai.DTS)
							{
								myVFG.Visible = true;
								myVFG.BringToFront();
								myRTB.Visible = false;
								using (StringReader sr = new StringReader(gai.Data))
								{
									myVFG.ReadXml(sr);
									sr.Close();
								}
								return;
							}
						}
					}
					else
					{
						myRTB.Visible = true;
						myRTB.BringToFront();
						myVFG.Visible = false;
						myRTB.SetupRichText(cai.Text, MyItemInfo.FormatStepData.Font);
					}
				}
				if (tn.Tag is ItemAuditInfo)
				{
					btnRestore.Enabled = true;
					ItemAuditInfo iai = tn.Tag as ItemAuditInfo;
					ContentAuditInfoList cail = ContentAuditInfoList.Get(iai.ContentID);
					foreach (ContentAuditInfo cai in cail)
					{
						if (cai.DeleteStatus == iai.DeleteStatus)
						{
							if (MyItemInfo.FormatStepData != null)
								myRTB.SetupRichText(cai.Text, MyItemInfo.FormatStepData.Font);
							else
								myRTB.SetupRichText(cai.Text, new VE_Font("arial", 12, E_Style.None));
						}
					}
				}
				if (tn.Tag is AnnotationAuditInfo)
				{
					btnRestore.Enabled = true;
					AnnotationAuditInfo iai = tn.Tag as AnnotationAuditInfo;
					myRTB.Rtf = iai.RtfText;
				}
			}
			else
			{
				btnRestore.Enabled = false;
				myRTB.Clear();
				myVFG.Clear();
			}
		}

		private void tvAudits_AfterSelect(object sender, TreeViewEventArgs e)
		{
			DisplayTagRTF(e.Node);
		}

		private void btnRestore_Click(object sender, EventArgs e)
		{
			TreeNode tn = tvAudits.SelectedNode;
			if (tn.Tag is ContentAuditInfo)
			{
				ContentAuditInfo cai = tn.Tag as ContentAuditInfo;
				if (MessageBox.Show(this, "Do you want to restore this content change?", "Confirm Content Change Restore", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					//TODO: need to fix this (dataportal stuff)
					ContentInfo ci = ContentInfo.RestoreContent(cai);
					UpdateHistory();
					myRTB.Clear();
					myVFG.Clear();
				}
			}
			if (tn.Tag is ItemAuditInfo)
			{
				ItemAuditInfo iai = tn.Tag as ItemAuditInfo;
				if (MessageBox.Show(this, "Do you want to restore this item?", "Confirm Item Restore", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					ItemInfo ii = MyItemInfo.RestoreSibling(iai);
					UpdateHistory();
					myRTB.Clear();
					myVFG.Clear();
				}
			}
			if (tn.Tag is AnnotationAuditInfo)
			{
				AnnotationAuditInfo aai = tn.Tag as AnnotationAuditInfo;
				if (MessageBox.Show(this, "Do you want to restore this annotation?", "Confirm Annotation Restore", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					//TODO: need to fix this (dataportal stuff)
					AnnotationInfo ai = AnnotationInfo.RestoreAnnotation(aai);
					UpdateHistory();
					myRTB.Clear();
					myVFG.Clear();
				}
			}
		}

		private void myVFG_Click(object sender, EventArgs e)
		{

		}
	}
}
